package com.voxel.sense.infomsg.module.publish;

import android.content.Context;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.example.com.common.view.adapter.TBaseAdapter;
import com.voxel.sense.infomsg.R;
import cyy.example.com.facotry.model.api.response.FirmNameByIdEntity;

import java.util.List;

/**
 * Created by ChenYiYao on 2018/9/3.
 */
public class FirmSelectAdapter extends TBaseAdapter<FirmNameByIdEntity> implements Filterable {

    private CustomFilter mFilter;

    public FirmSelectAdapter(Context context, List<FirmNameByIdEntity> firmNameByIdEntities) {
        super(context, firmNameByIdEntities,  R.layout.item_recycler_textview);
    }

    @Override
    public void initView(ViewHolder holder) {
        holder.addView(R.id.iv_desc_icon);
    }

    @Override
    public void setViewValue(ViewHolder holder, int position) {
        TextView textView = holder.getTextView(R.id.iv_desc_icon);
        FirmNameByIdEntity entity = list.get(position);
        textView.setText(entity.getCompanyName());

    }

    @Override
    public Filter getFilter() {
        if (mFilter == null) {
            mFilter = new CustomFilter();
        }
        return mFilter;
    }
//
    private class CustomFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence prefix) {
            FilterResults results = new FilterResults();
            List<FirmNameByIdEntity> list = getList();
            results.values = list;
            results.count = list.size();
            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    }
}
